// Car/ GameInfo Model

(function( window, undefined ) {

var GameFPS = 60;

var GameStatus = {
	Unknown:0,
	
	GameEnd:40,
	GameOver:44,
	
	Ready:99,
	
	Stopped:101,
	Stopping:190,
	
	Warming:201,
	Playing:301,
	Jumping:311
}

var GameLevelInfo = Class({
	name: "GameLevelInfo",
	parent: "Model",
	constructor: function() {
		var _speed = 8;
		var _gameData = [];
		var _eventData = [];
		
		return {
			__construct: function( gameData, speed ) {
				console.log( this, "__construct" );
				
				_speed = speed;
				_gameData = gameData;
				_eventData = _gameData.concat();
			},
			
			reset: function() {
				_eventData = _gameData.concat();
			},
			
			speed: function() {
				return _speed;
			},
			
			checkEvent: function( distance ) {
				var eventDistance = ( _eventData.length > 0 ) ? _eventData[0] * 100 : 0;
				
				//console.log( this, "checkEvent", eventDistance, distance );
				
				if ( eventDistance > 0 && distance >= eventDistance ) {
					_eventData.shift();
					
					return true;
				};
				
				return false;
			},
			
			isClear:function() {
				return ( _eventData.length == 0 ) ? true : false;
			}
		};
	}
});

var GameEvent = {
	UpdateStatus: "GameEvent.UpdateStatus",
	UpdateDistance: "GameEvent.UpdateDistance"
};

var GameInfo = Class({
	name: "GameInfo",
	parent: "Model",
	constructor: function() {
		var _status = GameStatus.Unknown;
		var _color = Class.View.Car.CarColor.red;
		var _distance = 0;
		var _totalDistance = 0;
		
		var _speed = 0;
		var _level = 0;
		
		var _levelData = [
			//new GameLevelInfo( [2], 8 )
			new GameLevelInfo( [2, 4, 6, 8, 10], 8 ),
			new GameLevelInfo( [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 8 ),
			new GameLevelInfo( [2, 4, 6, 8, 10], 12 )
		];
		
		this.levelInfo = undefined;
		
		return {
			__construct: function() {
				console.log( this, "__construct" );
				
				console.log( Helper.EventDispatcher );
				
				Helper.EventDispatcher( this );
			},
			
			ready: function() {
				console.log( this, "ready" );
				
				_totalDistance = 0;
				_distance = 0;
				_level = 0;
				
				for ( var i=0,l=_levelData.length;i<l;i++ ) {
					var levelInfo = _levelData[i];
					levelInfo.reset();
				};
		
				this.levelInfo = _levelData[_level];
				
				this.status( GameStatus.Ready );
			},
			
			speed: function( speed ) {
				
				var max = 0;
			
				if ( _status == GameStatus.Stopping ) {
					max = 40;
				}
				else if ( _status == GameStatus.Warming || _status >= GameStatus.Playing ) {
					max = this.levelInfo.speed() * 10;
				};
				
				if ( _speed > max ) {
					_speed = _speed - parseInt( ( _speed - max ) / 10 );
				}
				else if ( _speed < max ) {
					_speed = _speed + parseInt( ( max - _speed ) / 10 );
				}
				
				if ( _speed < 0 ) {
					_speed = 0;
				};
				
				return ( _speed == 0 ) ? 0 : _speed / 10;
			},
			
			levelData: function() {
				return _levelData;
			},
			
			nextLevel: function() {
				_totalDistance = _distance;
			
				_distance = 0;
				_level = _level + 1;
				
				if ( _levelData[_level] ) {
					this.levelInfo = _levelData[_level];
				}
				else {
					this.gameOver();
				};
			},
			
			gameOver:function() {
				this.status( GameStatus.GameOver );
			},
			
			color: function() {
				return _color;
			},
			
			level: function() {
				return _level;
			},
			
			distance: function( distance ) {
				if ( arguments.length == 0 ) { 
					return _distance;
				};
				
				_distance = distance;
				
				//console.log( this, "distance", _distance );
				
				this.dispatchEvent( GameEvent.UpdateDistance, _distance );
			},
			
			totalDistance: function() {
				return _totalDistance + _distance;
			},
			
			statusName: function() {
				for ( var key in GameStatus ) {
					if ( GameStatus[key] == _status ) {
						return key;
					};
				};
				
				return "";
			},
			
			status: function( status ) {
				if ( arguments.length == 0 ) {
					return _status;
				};
				
				if ( _status == status ) {
					return;
				};
				
				console.log( this, "status", _status, "=>", status );
				
				_status = status;
				
				this.dispatchEvent( GameEvent.UpdateStatus, _status );
			},
		
			changeColor: function( color ) {
				console.log( this, "changeColor", color );
				
				_color = color;
			},
			
			isReady: function() {
				return ( _status == GameStatus.Ready ) ? true : false;
			},
			
			isPlaying: function() {
				return ( _status >= GameStatus.Warming ) ? true : false;
			},
			
			isJumping: function() {
				return ( _status >= GameStatus.Jumping ) ? true : false;
			},
			
			isGameOver: function() {
				return ( _status <= GameStatus.GameOver ) ? true : false;
			}
		};
	}
});

GameInfo.GameFPS = GameFPS;
GameInfo.GameEvent = GameEvent;
GameInfo.GameStatus = GameStatus;

Class.Model.GameInfo = GameInfo;


})(window);